<template>
  <div class="login">
    <el-form ref="loginForm" :model="loginForm" class="login-form" :rules="loginRules">
      <h3 class="title">品新后台管理系统</h3>
      <div class="flex">
        <div id="login_container"></div>
        <div class="account_box">
          <el-form-item label="" prop="username">
            <el-input placeholder="用户名" v-model="loginForm.username" autocomplete="off">
              <template slot="prepend"><i class="el-icon-user-solid"></i></template>
            </el-input>
          </el-form-item>
          <el-form-item label="" prop="password" class="relative">
            <el-input type="password" placeholder="密码" v-model="loginForm.password" show-password autocomplete="off">
              <template slot="prepend"><i class="el-icon-s-ticket"></i></template>
            </el-input>
            <!-- <div style="font-size: 12px; right: 10px" class="finger absolute">忘记密码</div> -->
          </el-form-item>
          <el-form-item label="">
            <el-button style="width: 100%; margin-top: 40px" type="primary"  @click="submitLogin()">登 入</el-button>
            <!-- <el-button style="width: 100%; margin-top: 20px; margin-left: 0px">注 册</el-button> -->
          </el-form-item>
        </div>
      </div>
    </el-form>
  </div>
</template>

<script>
// import { getAccessToken } from '@/api/autoToken'
import validator from '@/utils/jstar/checkPassword'
export default {
  name: 'Login',
  data() {
    let checkPassword = (rule, value, callback) => {
      if (!value) {
        callback(new Error('密码不能为空'))
        return
      }
      let flag = validator(value)
      if (flag) {
        callback(new Error(flag))
        return
      }

      callback()
    }
    return {
      loginForm: {
        username: '',
        password: '',
        rememberMe: false,
        code: '',
        uuid: '',
      },
      loginRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          // { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, validator: checkPassword, trigger: 'blur' }
        ],
      }
    }
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true,
    },
  },
  created () {
  },
  mounted () {
    console
    DDLogin({
      id:"login_container",
      goto: encodeURIComponent("https://oapi.dingtalk.com/connect/oauth2/sns_authorize?appid=dingzlqcc6js24tdolxp&response_type=code&scope=snsapi_login&state=STATE&redirect_uri=" + window.webUrl),
      style: "border:none;background-color:#FFFFFF;",
      width : "365",
      height: "400"
    });
    var handleMessage = function (event) {
      var origin = event.origin;
      // console.log("origin", event.origin);
      if( origin == "https://login.dingtalk.com" ) { //判断是否来自ddLogin扫码事件。
        var loginTmpCode = event.data; 
        //获取到loginTmpCode后就可以在这里构造跳转链接进行跳转了
        // console.log("loginTmpCode", loginTmpCode);
        window.location.href= 'https://oapi.dingtalk.com/connect/oauth2/sns_authorize?appid=SuiteKey&response_type=code&scope=snsapi_login&state=STATE&redirect_uri='+ window.webUrl + '&loginTmpCode=' + loginTmpCode
      }
    };
    if (typeof window.addEventListener != 'undefined') {
        window.addEventListener('message', handleMessage, false);
    } else if (typeof window.attachEvent != 'undefined') {
        window.attachEvent('onmessage', handleMessage);
    }
  },
  methods: {
    submitLogin () {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          // alert('submit!');
          this.handleLogin()
          // setTimeout(() => {
          //   this.$refs.loginForm.fields[1].validateDisabled = false
          //   this.$refs.loginForm.fields[1].validateMessage = "用户名或者密码错误，请重新输入！"
          //   this.$refs.loginForm.fields[1].validateState = 'error'
          // }, 200)
        } else {
          return false;
        }
      })
    },
    handleLogin() {
      this.$store
        .dispatch('Login', this.loginForm)
        .then((res) => {
          if (res) {
            this.$refs.loginForm.fields[1].validateDisabled = false
            this.$refs.loginForm.fields[1].validateMessage = res.code + '-' +res.msg
            this.$refs.loginForm.fields[1].validateState = 'error'
            return
          }
          // this.$router.push({ path: this.redirect.split('?')[0] || '/' }).catch(() => {})
          this.$router.push({ path: '/index' }).catch(() => {})
        })
        .catch(() => {
          this.loading = false
        })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss">
.login {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  background-image: url('../assets/images/login-background.jpg');
  background-size: cover;
}
.title {
  margin: 0px auto 30px auto;
  text-align: center;
  color: #707070;
}

.login-form {
  border-radius: 6px;
  background: #ffffff;
  // width: 400px;
  padding: 25px 25px 5px 25px;
  .el-input {
    height: 38px;
    input {
      height: 38px;
    }
  }
  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 2px;
  }
  .account_box{
    width: 365px;
    height: 400px;
    padding: 60px 40px 0 40px;
    box-sizing: border-box;
    flex-wrap: wrap;
    position: relative;
    &::before{
      display: block;
      content: '';
      position: absolute;
      left: -15px;
      top: 10px;
      width: 1px;
      height: 80%;
      background-color: #eeeeee;
    }
    input{
      background-color: #f5f6f7;
      border: unset;
    }
    .el-input-group__prepend{
      border: unset;
      color: #1890ff;
    }
  }
}
.login-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}
.login-code {
  width: 33%;
  height: 38px;
  float: right;
  img {
    cursor: pointer;
    vertical-align: middle;
  }
}
.el-login-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}
.login-code-img {
  height: 38px;
}
</style>
